<?php
/**
 * Main index file, this file will always be loaded first
 *
 *
 */

// Creat a session for obvious purposes
session_start();

// Define security constants
define('SECUR_CHECK', TRUE);

// Including general functions
require_once ("functions.inc");

// Including configuration file
require_once ('configuration.php');

// Including standard classes
require_once ('includes/classes/db.class.php');
require_once ('includes/classes/site.class.php');
require_once ('includes/classes/user.class.php');
require_once ('includes/classes/session.class.php');
require_once ('includes/smarty/Smarty.class.php');
require_once ('includes/classes/validator.class.php');

// Loading required classes
$conf = new configuration();
$database = new database();


// Create constants
define("RELPATH", $conf->root_path);
define("ABSPATH", dirname(__FILE__));

/*
 * Everything's loaded, lets go !
 */

// Check for current user
$user = false;
if(isset($_SESSION['loggedin']) && isset($_SESSION['uid']))
{
    $user = new user($_SESSION['uid']);
}
else
{
    $user = new user();
}

// Load global site & current page data
$site = new site();

// initial global scripts
$site->add_script("/javascript/jquery.js");
$site->add_script("/includes/jquery_ui/js/jquery-ui-1.8.2.js");
$site->add_script("/javascript/superfish.js");

// initial global styles
$site->add_style("/includes/jquery_ui/css/redmond/jquery-ui-1.8.2.custom.css");

// Setup smarty (template engine)
define("TEMPLATE_PATH", ABSPATH.'/templates/'.$site->template);
$smarty = new Smarty();
$smarty->template_dir   =   TEMPLATE_PATH;

/*
        -- THE ACTUAL PAGE INCLUDED -----------------
 */
include_once(ABSPATH."/includes/pages/".$site->currentpage.".php");
/*
        ------------------------------------------------
 */

// add final scripts
$site->add_script("/javascript/brm.js");

// Load appropiate menu
$menu = $site->build_menu();

// Assigning misc smarty variables
$smarty->assign("MENU", $menu);

// Check if user is logged in, for login message
if($user->loggedin)
{
    $message = "Logged in as ".$user->profile[0]['prof_fullname']." [<a href='".RELPATH."/logout'>logout</a>]";
}
else
{
    $message = "You are not logged in, <a href='".RELPATH."/login'>login</a> or <a href='register'>register</a>";
}
$smarty->assign("STYLES", $site->styles);
$smarty->assign("MESSAGE", $message);
$smarty->assign("SCRIPTS", $site->scripts);
$smarty->assign('TEMPLATE_PATH', RELPATH.'/templates/'.$site->template);
$smarty->assign("RELPATH", RELPATH);

if(is_array($site->tpl_to_load))
{
    foreach ($site->tpl_to_load as $template)
    {
        $smarty->display($template);
    }
}
else
{
    echo "Error loading page...";
}
?>
